package cn.iocoder.yudao.module.promotion.service.paymentterm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.paymentterm.PaymentTermDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.promotion.dal.mysql.paymentterm.PaymentTermMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;

/**
 * 收款方式 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PaymentTermServiceImpl implements PaymentTermService {

    @Resource
    private PaymentTermMapper paymentTermMapper;

    @Override
    public Long createPaymentTerm(PaymentTermSaveReqVO createReqVO) {
        // 插入
        PaymentTermDO paymentTerm = BeanUtils.toBean(createReqVO, PaymentTermDO.class);
        paymentTerm.setOrderCount(0L);
        paymentTermMapper.insert(paymentTerm);
        // 返回
        return paymentTerm.getId();
    }

    @Override
    public void updatePaymentTerm(PaymentTermSaveReqVO updateReqVO) {
        // 校验存在
        validatePaymentTermExists(updateReqVO.getId());
        // 更新
        PaymentTermDO updateObj = BeanUtils.toBean(updateReqVO, PaymentTermDO.class);
        paymentTermMapper.updateById(updateObj);
    }

    @Override
    public void deletePaymentTerm(Long id) {
        // 校验存在
        validatePaymentTermExists(id);
        // 删除
        paymentTermMapper.deleteById(id);
    }

    private void validatePaymentTermExists(Long id) {
        if (paymentTermMapper.selectById(id) == null) {
            throw exception(PAYMENT_TERM_NOT_EXISTS);
        }
    }

    @Override
    public PaymentTermDO getPaymentTerm(Long id) {
        return paymentTermMapper.selectById(id);
    }

    @Override
    public PaymentTermDO getPaymentTermByMemberId(Long memberId) {
        return paymentTermMapper.selectOne(new LambdaQueryWrapper<PaymentTermDO>()
                .eq(PaymentTermDO::getMemberId, memberId));
    }

    @Override
    public PageResult<PaymentTermDO> getPaymentTermPage(PaymentTermPageReqVO pageReqVO) {
        return paymentTermMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PaymentTermDO> getCompanyPaymentTerm(PaymentTermPageReqVO pageReqVO) {
        return paymentTermMapper.selectList(new LambdaQueryWrapper<PaymentTermDO>()
                .eq(PaymentTermDO::getIsCompany, pageReqVO.getIsCompany()));
    }

    @Override
    public List<PaymentTermDO> getLockUserPaymentTerm(PaymentTermPageReqVO pageReqVO) {
        return paymentTermMapper.selectList(new LambdaQueryWrapper<PaymentTermDO>()
                .inSql(PaymentTermDO::getMemberId, "select id from member_user where is_lock = 1"));
    }

}